﻿
//
// Copyright (c) 2024 BPer
//
#ifndef SRC_BPERUTIL_SRC_BPERTIMER_H_
#define SRC_BPERUTIL_SRC_BPERTIMER_H_
#include <bperutil/sys/BPerThread2.h>
#include <bperutil/BPerType.h>

#if !BPer_Enable_Timer_Phtread
#if BPer_OS_WIN
#include <windows.h>
#endif
#endif

class BPerTimerTask {
public:
	BPerTimerTask() {};
	virtual ~BPerTimerTask() {};
	virtual void doTask(int32_t taskId)=0;
};


class BPerTimer: public BPerThread {
public:
	BPerTimer();
	virtual ~BPerTimer();
	int32_t m_isStart;
	int32_t m_loop;
	int32_t m_waitState;
	int32_t m_waitTime;

	void setTask(BPerTimerTask *ptask);
	void setTimelen(int32_t ptimelen);//hao miao


	void setTaskId(int32_t ptaskId);
	void stop();

#if !BPer_Enable_Timer_Phtread
#if BPer_OS_WIN
	static void  CALLBACK TimeEvent(PVOID lpParam, BOOLEAN TimerOrWaitFired);
#endif
#endif

protected:
	void run();
	void startLoop();
	void stopLoop();
private:
    BPerTimerTask *m_task;
    int32_t m_taskId;



#if BPer_Enable_Timer_Phtread
    bPer_thread_mutex_t m_lock;
    bPer_thread_cond_t m_cond_mess;
#else
#if BPer_OS_WIN
        void startWindowsEventTime(int pwaitTime,DWORD_PTR duser);
	HANDLE	m_hTimerQueue;
	HANDLE	m_hTimerQueueTimer;
	HANDLE m_winEvent;
#else
	int m_timerfd;
	int m_efd;
#endif
#endif

};

#endif /* SRC_BPERUTIL_SRC_BPERTIMER_H_ */
